$(function () {
    var lis = $('#nav li');
    var pages = $('#container .page');
    lis.click(function () {
        $(this).addClass('active').siblings().removeClass('active');
        pages.eq($(this).index()).addClass('show').siblings().removeClass('show');
    });
    $('#btn-create').click(function () {
        var pwd = $('#input-create').val().trim();
        if (pwd.length == 0) {
            alert('请填写密码');
        } else {
            $.get('/wallet/create', {pwd: pwd}, function (data) {
                if (data.code == 0 && data.info.length > 0) {
                    var rs = data.info[0];
                    var dom = $('#res-create');
                    dom.append('<div class="res-item"><span>钱包地址:</span>&nbsp&nbsp' + rs.address + '</div>');
                    dom.append('<div class="res-item"><span>私钥:</span>&nbsp&nbsp' + rs.privateKey + '</div>');
                    dom.append('<div class="res-item"><span>助记词:</span>&nbsp&nbsp' + rs.seed + '</div>');
                    dom.append('<div class="res-item"><span>keystore:</span>&nbsp&nbsp' + JSON.stringify(rs.keystore) + '</div>');
                    dom.append('<div class="res-item"><span>keystore文件名:</span>&nbsp&nbsp' + rs.keystoreName + '</div>');
                    dom.append('<div class="res-item"><span>keystore密码:</span>&nbsp&nbsp' + rs.keystorePwd + '</div>');
                } else {
                    alert(data.msg);
                }
            })
        }
    });
    $('#btn-pri').click(function () {
        var pri = $('#input-pri').val().trim();
        var pwd = $('#input-pri-pwd').val().trim();
        if (pri.length == 0) {
            alert('请填写私钥');
            return;
        }
        if (pwd.length == 0) {
            alert('请填写密码');
            return;
        }
        $.get('/wallet/importPriKey', {pri: pri, pwd: pwd}, function (data) {
            if (data.code == 0 && data.info.length > 0) {
                var rs = data.info[0];
                var dom = $('#res-pri');
                dom.append('<div class="res-item"><span>钱包地址:</span>&nbsp&nbsp' + rs.address + '</div>');
                dom.append('<div class="res-item"><span>私钥:</span>&nbsp&nbsp' + rs.privateKey + '</div>');
                dom.append('<div class="res-item"><span>keystore:</span>&nbsp&nbsp' + JSON.stringify(rs.keystore) + '</div>');
                dom.append('<div class="res-item"><span>keystore文件名:</span>&nbsp&nbsp' + rs.keystoreName + '</div>');
                dom.append('<div class="res-item"><span>keystore密码:</span>&nbsp&nbsp' + rs.keystorePwd + '</div>');
            } else {
                alert(data.msg);
            }
        })
    });
    $('#btn-seed').click(function () {
        var seed = $('#input-seed').val().trim();
        var pwd = $('#input-seed-pwd').val().trim();
        if (seed.length == 0) {
            alert('请填写助记词');
            return;
        }
        if (pwd.length == 0) {
            alert('请填写密码');
            return;
        }
        $.get('/wallet/importSeed', {seed: seed, pwd: pwd}, function (data) {
            if (data.code == 0 && data.info.length > 0) {
                var rs = data.info[0];
                var dom = $('#res-seed');
                dom.append('<div class="res-item"><span>钱包地址:</span>&nbsp&nbsp' + rs.address + '</div>');
                dom.append('<div class="res-item"><span>私钥:</span>&nbsp&nbsp' + rs.privateKey + '</div>');
                dom.append('<div class="res-item"><span>助记词:</span>&nbsp&nbsp' + rs.seed + '</div>');
                dom.append('<div class="res-item"><span>keystore:</span>&nbsp&nbsp' + JSON.stringify(rs.keystore) + '</div>');
                dom.append('<div class="res-item"><span>keystore文件名:</span>&nbsp&nbsp' + rs.keystoreName + '</div>');
                dom.append('<div class="res-item"><span>keystore密码:</span>&nbsp&nbsp' + rs.keystorePwd + '</div>');
            } else {
                alert(data.msg);
            }
        })
    });
    $('#btn-keystore').click(function () {
        var keystore = $('#input-keystore').val().trim();
        var pwd = $('#input-keystore-pwd').val().trim();
        if (keystore.length == 0) {
            alert('请填写keystore');
            return;
        }
        if (pwd.length == 0) {
            alert('请填写密码');
            return;
        }
        $.get('/wallet/importKeystore', {keystore: keystore, pwd: pwd}, function (data) {
            if (data.code == 0 && data.info.length > 0) {
                var rs = data.info[0];
                var dom = $('#res-keystore');
                dom.append('<div class="res-item"><span>钱包地址:</span>&nbsp&nbsp' + rs.address + '</div>');
                dom.append('<div class="res-item"><span>私钥:</span>&nbsp&nbsp' + rs.privateKey + '</div>');
                dom.append('<div class="res-item"><span>keystore:</span>&nbsp&nbsp' + JSON.stringify(rs.keystore) + '</div>');
                dom.append('<div class="res-item"><span>keystore密码:</span>&nbsp&nbsp' + rs.keystorePwd + '</div>');
            } else {
                alert(data.msg);
            }
        })
    });

    $('#btn-eth').click(function () {
        var addr = $('#input-eth-addr').val().trim();
        if (addr.length == 0) {
            alert('请填写钱包地址');
            return;
        }
        $.get('/wallet/getEthBalance', {addr: addr}, function (data) {
            if (data.code == 0 && data.info.length > 0) {
                var rs = data.info[0];
                var dom = $('#res-eth');
                dom.append('<div class="res-item"><span>eth余额:</span>&nbsp&nbsp' + rs.balance + '</div>');
            } else {
                alert(data.msg);
            }
        })
    });

    $('#btn-token').click(function () {
        var addr = $('#input-token-addr-1').val().trim();
        if (addr.length == 0) {
            alert('请填写钱包地址');
            return;
        }
        var token = $('#input-token-addr-2').val().trim();
        if (token.length == 0) {
            alert('请填写代币地址');
            return;
        }
        $.get('/wallet/getTokenBalance', {addr: addr, token: token}, function (data) {
            if (data.code == 0 && data.info.length > 0) {
                var rs = data.info[0];
                var dom = $('#res-token');
                dom.append('<div class="res-item"><span>钱包地址:</span>&nbsp&nbsp' + rs.fromAddress + '</div>');
                dom.append('<div class="res-item"><span>代币地址:</span>&nbsp&nbsp' + rs.tokenAddress + '</div>');
                dom.append('<div class="res-item"><span>代币名称:</span>&nbsp&nbsp' + rs.tokenName + '</div>');
                dom.append('<div class="res-item"><span>代币精度:</span>&nbsp&nbsp' + rs.decimals + '</div>');
                dom.append('<div class="res-item"><span>代币符号:</span>&nbsp&nbsp' + rs.tokenSymbol + '</div>');
                dom.append('<div class="res-item"><span>代币余额:</span>&nbsp&nbsp' + rs.balance + '</div>');
            } else {
                alert(data.msg);
            }
        })
    });
    $('#btn-eth-tx').click(function () {
        var fromAddr = $('#input-eth-tx-1').val().trim();
        if (fromAddr.length == 0) {
            alert('请输入出款方地址');
            return;
        }
        var pri = $('#input-eth-tx-2').val().trim();
        if (pri.length == 0) {
            alert('请输入出款方私钥');
            return;
        }
        var toAddr = $('#input-eth-tx-3').val().trim();
        if (toAddr.length == 0) {
            alert('请输入收款方地址');
            return;
        }
        var amount = $('#input-eth-tx-4').val().trim();
        var reg = /^(([1-9][0-9]*\.[0-9][0-9]*)|([0]\.[0-9][0-9]*)|([1-9][0-9]*)|([0]{1}))$/;
        if (!reg.test(amount) || !parseFloat(amount) > 0) {
            alert('请输入正确的金额');
            return;
        }
        $.get('/wallet/sendEthTransaction', {
            fromAddr: fromAddr,
            pri: pri,
            toAddr: toAddr,
            amount: amount
        }, function (data) {
            if (data.code == 0 && data.info.length > 0) {
                var rs = data.info[0];
                var dom = $('#res-eth-tx');
                dom.append('<div class="res-item"><span>出款方:</span>&nbsp&nbsp' + rs.from + '</div>');
                dom.append('<div class="res-item"><span>收款方:</span>&nbsp&nbsp' + rs.to + '</div>');
                dom.append('<div class="res-item"><span>转账金额:</span>&nbsp&nbsp' + rs.amount + '</div>');
                dom.append('<div class="res-item"><span>交易hash:</span>&nbsp&nbsp' + rs.hash + '</div>');
            } else {
                alert(data.msg);
            }
        })
    });
    $('#btn-token-tx').click(function () {
        var fromAddr = $('#input-token-tx-1').val().trim();
        if (fromAddr.length == 0) {
            alert('请输入出款方地址');
            return;
        }
        var pri = $('#input-token-tx-2').val().trim();
        if (pri.length == 0) {
            alert('请输入出款方私钥');
            return;
        }
        var toAddr = $('#input-token-tx-3').val().trim();
        if (toAddr.length == 0) {
            alert('请输入收款方地址');
            return;
        }
        var tokenAddr = $('#input-token-tx-4').val().trim();
        if (tokenAddr.length == 0) {
            alert('请输入代币合约地址');
            return;
        }
        var amount = $('#input-token-tx-5').val().trim();
        var reg = /^(([1-9][0-9]*\.[0-9][0-9]*)|([0]\.[0-9][0-9]*)|([1-9][0-9]*)|([0]{1}))$/;
        if (!reg.test(amount) || !parseFloat(amount) > 0) {
            alert('请输入正确的金额');
            return;
        }
        $.get('/wallet/sendTokenTransaction', {
            fromAddr: fromAddr,
            pri: pri,
            toAddr: toAddr,
            tokenAddr:tokenAddr,
            amount: amount
        }, function (data) {
            if (data.code == 0 && data.info.length > 0) {
                var rs = data.info[0];
                var dom = $('#res-token-tx');
                dom.append('<div class="res-item"><span>出款方:</span>&nbsp&nbsp' + rs.from + '</div>');
                dom.append('<div class="res-item"><span>收款方:</span>&nbsp&nbsp' + rs.to + '</div>');
                dom.append('<div class="res-item"><span>转账金额:</span>&nbsp&nbsp' + rs.amount + '</div>');
                dom.append('<div class="res-item"><span>代币地址:</span>&nbsp&nbsp' + rs.tokenAddr + '</div>');
                dom.append('<div class="res-item"><span>交易hash:</span>&nbsp&nbsp' + rs.hash + '</div>');
            } else {
                alert(data.msg);
            }
        })
    });
});